﻿using System;
using System.Collections.Generic;
using System.Text;
using Log4Nez.Interfaces;
using System.Xml;
using Log4Nez.Enumerations;
using System.Collections.Specialized;
using Log4Nez.Loggers;

namespace Log4Nez.Factories
{
    internal static class FileLoggerFactory
    {
        public static ILogger CreateFileLogger(XmlNode loggerNode, LoggerType logType)
        {
            ILogger logger = null;
            string stringClassType = loggerNode.SelectSingleNode("PropertiesMapping") != null ? loggerNode.SelectSingleNode("PropertiesMapping").Attributes["class"].Value : null;
            if (!string.IsNullOrEmpty(stringClassType))
            {
                //Type classType = Type.GetType(stringClassType);
                Dictionary<string, string> properties = new Dictionary<string, string>();
                XmlNodeList nodiProperty = loggerNode.SelectNodes("PropertiesMapping/add");
                foreach (XmlNode property in nodiProperty)
                {
                    properties.Add(property.Attributes["property"].Value, property.Attributes["column"].Value);
                }
                XmlAttribute filepath = loggerNode.Attributes["filelocation"];
                XmlAttribute filenameFormat = loggerNode.Attributes["filenameFormat"];
                logger = new CsvFileLogger(properties,
                    filenameFormat != null ? filenameFormat.Value : AppDomain.CurrentDomain.ApplicationIdentity.FullName + ".log",
                    filepath != null ? filepath.Value : AppDomain.CurrentDomain.BaseDirectory);
                //logger = new CsvFileLogger(properties, filenameFormat != null ? filenameFormat.Value : AppDomain.CurrentDomain.ApplicationIdentity.FullName + ".log" ); //TODO: Applicare il nome dell'applicazione in esecuzione come nome predefinito del file di log
            }
            return logger;
        }
    }
}
